package br.com.materiais.factory;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class MaterialConnectionFactory 
{
  private static  SessionFactory sessionFactory = null;
  
  private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
  
  static
  {
	  try
	  {
		  sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory( );
	  }
	  catch(Throwable t)
	  {
		  throw new ExceptionInInitializerError(t);
	  }
	  
  }
  
  public static Session getInstance()
  {
	  Session session = (Session)threadLocal.get( );
	  
	  session = sessionFactory.openSession( );
	  
	  threadLocal.set(session);
	  
	  return session;
  }
  
}
